home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-04-16 | 7.2 KB | 192 lines | [TEXT/CWIE] |
- // IAStorage.h
- // Copyright: © 1994 - 1998 by Apple Computer, Inc., all rights reserved.
-
-
- // Manages allocation of variable-sized persistent blocks and atomic transactions.
-
- #pragma once
- #ifndef IAStorage_h
- #define IAStorage_h
-
- #pragma import on
-
- #if PRAGMA_STRUCT_ALIGN
- #pragma options align=power
- #endif
-
- #include "IAStoreStream.h"
-
- #pragma IA_BEGIN_EXPORTS
-
- typedef uint32 IABlockID;
- typedef uint32 IABlockSize;
- typedef uint32 IABlockAddress;
-
- // This interface assumes that block IDs are not raw disk addresses, but rather
- // logical block numbers. This frees clients from having to shadow blocks
- // changed in a transaction, and also enables compaction without knowledge of
- // client data structures. The downside is that, to be efficient, the
- // table mapping block-IDs to block-addresses must be memory-resident.
- // So long as most blocks are reasonably large (around 1kb) the memory
- // requirements should not be too bad.
-
- class IAStorage : public IAObject {
- friend class IAOutputBlock;
- friend class IAInputBlock;
- public:
- // constructor: notes storeStream & type, creates mutex
- IAStorage(IAStoreStream* s, uint32 type);
- // destructor: deletes storeStream & mutex
- ~IAStorage();
-
- // Initializes a new storage, or empties an existing one.
- // The storage is left open afterwards.
- virtual void Initialize() = 0;
-
- // Opens the storage (and its storeStream), enabling subsequent operations.
- // If `writable' is true, destructive operations are supported.
- virtual void Open(bool writable = false) = 0;
- bool IsOpen() { return storeStream->IsOpen(); }
- bool IsWritable() { return storeStream->IsWritable(); }
-
- // Makes permanent any changes since open.
- virtual void Commit() = 0;
-
- // Allocates a new block.
- virtual IABlockID Allocate() = 0;
-
- // Frees a previously allocated block.
- virtual void Deallocate(IABlockID id) = 0;
-
- // The TOC (table of contents) maintains a list of named block IDs.
- // This enables clients to find their data structures, which may have
- // different block IDs in different storages.
- virtual void TOC_Set(const char* label, IABlockID id) = 0;
- virtual IABlockID TOC_Get(const char* label) = 0;
- virtual bool TOC_Remove(const char* label) = 0;
- /// Convenient functions capturing common TOC idioms:
- // Adds a new TOC entry whose value is a newly allocated block ID.
- IABlockID AllocateNamedBlock(const char* name);
- // If an entry already exists, returns it, otherwise calls AllocateNamedBlock().
- IABlockID GetNamedBlock(const char* name);
- // If an entry exists, remove it.
- bool RemoveNamedBlock(const char* name);
-
- // The total number of bytes in the storage and free, respectively.
- virtual IABlockSize TotalSpace() = 0;
- virtual IABlockSize FreeSpace() = 0;
-
- // Attempts to compact the storage
- virtual void Compact() = 0;
-
- // in the debug libraries, prints some info about the set to the standard output
- virtual void PrintFreeList();
- IAStoreStream* GetStoreStream() const {return storeStream;}
- const uint32 GetStorageType() const {return storageType;}
- IAMutex* GetMutex() const {return mutex;}
-
- protected:
- /// The following called only by InputBlock & OutputBlock (our friends).
- /// Clients should always access block contents through InputBlock & OutputBlock.
-
- // Returns the address that a block may be read from.
- virtual IABlockAddress GetReadAddress(IABlockID id) = 0;
-
- // Returns the address that a block may be written to.
- virtual IABlockAddress GetWriteAddress(IABlockID id, IABlockSize size) = 0;
-
- // Returns the number of bytes currently allocated for a block.
- virtual IABlockSize GetBlockSize(IABlockID id) = 0;
-
- //// Give subclasses direct access to IAStoreStreams.
- void InitializeSS(IAStoreStream* s) { s->Initialize(); }
- void OpenSS(IAStoreStream* s, bool writeable) { s->Open(writeable); }
- void FlushSS(IAStoreStream* s) { s->Flush(); }
- uint32 GetEOF(IAStoreStream* s) { return s->GetEOF(); }
- void SetEOF(IAStoreStream* s, uint32 address) { s->SetEOF(address); }
- void Write(IAStoreStream* s, uint32 a, const byte* d, uint32 l) { s->Write(a,d,l); }
- uint32 Read(IAStoreStream* s, uint32 a, byte* d, uint32 l) { return s->Read(a,d,l); }
- uint32 GetPosition(IAStoreStream* s) { return s->GetPosition(); }
- void SetPosition(IAStoreStream* s, uint32 a, uint32 b) { s->SetPosition(a, b); }
- void WriteByte(IAStoreStream* s, byte b) { s->WriteByte(b); }
- byte ReadByte(IAStoreStream* s) { return s->ReadByte(); }
- void WriteUInt32(IAStoreStream* s, uint32 i) { s->WriteUInt32(i); }
- uint32 ReadUInt32(IAStoreStream* s) { return s->ReadUInt32(); }
- void WriteBytes(IAStoreStream* s, const void* b, uint32 l) { s->WriteBytes(b,l); }
- void ReadBytes(IAStoreStream* s, void* b, uint32 l) { s->ReadBytes(b,l); }
- IAMutex* SSMutex(IAStoreStream* s) { return s->mutex; }
- private:
- IAStorage(IAStorage&); // don't define a copy constructor
- IAStoreStream* storeStream; // used for raw i/o
- const uint32 storageType; // identifies the format used by this storage
- IAMutex* mutex; // to synchronize access
- };
-
- // This should be used instead of a constructor to make a default-format storage.
- IAStorage* IAMakeStorage(IAStoreStream* storeStream);
-
-
- // This class is allocated on the stack, as follows:
- // IAOutputBlock output(storage, blockID, size);
- class IAOutputBlock {
- public:
- // locks stream's mutex & positions stream at address for write
- // a cloned IAStoreStream can be supplied to improved threaded throughput
- IAOutputBlock(IAStorage* sto, IABlockID id, IABlockSize sz, IAStoreStream* stream = NULL);
- // flushes changes & unlocks stream's mutex
- ~IAOutputBlock();
-
- void WriteByte(byte b) { stream->WriteByte(b); }
- void WriteUInt32(uint32 i) { stream->WriteUInt32(i); }
- void WriteBuffer(const void* b, uint32 l){ stream->WriteBytes(b, l); }
-
- uint32 GetPosition() { return stream->GetPosition(); }
- private:
- IAStoreStream* stream;
-
- void* operator new(size_t size); // stack allocate only
- IAOutputBlock(IAOutputBlock&); // don't define copy constructor
- };
-
- // This class is allocated on the stack, as follows:
- // IAInputBlock input(storage, blockID);
- class IAInputBlock {
- public:
- // locks stream's mutex & positions stream at address for read
- // a cloned IAStoreStream can be supplied to improved threaded throughput
- IAInputBlock(IAStorage* sto, IABlockID id, IAStoreStream* stream = NULL);
- // unlocks stream's mutex
- ~IAInputBlock();
-
- byte ReadByte() { return stream->ReadByte(); }
- uint32 ReadUInt32() { return stream->ReadUInt32(); }
- void ReadBuffer(void* b, uint32 l) { stream->ReadBytes(b, l); }
-
- uint32 GetPosition() { return stream->GetPosition(); }
- private:
- IAStoreStream* stream;
-
- void* operator new(size_t size); // stack allocate only
- IAInputBlock(IAInputBlock&); // don't define copy constructor
- };
-
-
- IAExceptionCode StorageInvalid = 'VSIV';
- IAExceptionCode StorageNotInitialized = 'VSNI';
- IAExceptionCode StorageNotOpen = 'VSNO';
- IAExceptionCode StorageAlreadyOpen = 'VSAO';
-
- IAExceptionCode StorageFull = 'VSDF';
- IAExceptionCode StorageNotWritable = 'VSNW';
- IAExceptionCode StorageBlockIDInvalid = 'VSBI';
- IAExceptionCode StorageBlockNameAllocated = 'VSNT';
-
- #pragma IA_END_EXPORTS
-
- #if PRAGMA_STRUCT_ALIGN
- #pragma options align=reset
- #endif
-
- #pragma import reset
- #endif
-